package com.carkeeper.shop.service.impl;

import com.carkeeper.shop.dao.CatalogueMapper;
import com.carkeeper.shop.pojo.Catalogue;
import com.carkeeper.shop.check.dao.TypeMapper;
import com.carkeeper.shop.check.dao.StoreMapper;
import com.carkeeper.shop.service.CatalogueService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.HashMap;
import java.util.Map;

/**
 * 服务目录查询
 */
@Service
public class CatalogueServiceImpl implements CatalogueService {

    @Autowired
    private CatalogueMapper catalogueMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private TypeMapper typeMapper;
    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Catalogue> findAll(int page, int size) {

        PageHelper.startPage(page,size);
        //System.out.println();
        return (Page<Catalogue>) catalogueMapper.selectAll();
    }

    /**
     * 多条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Catalogue> findTermPage(Map<String, Object> searchMap, int page, int size) {
        Example example = createExample(searchMap);
        PageHelper.startPage(page,size);
        Page<Catalogue> cataloguePage = (Page<Catalogue>) catalogueMapper.selectByExample(example);
        return cataloguePage;
    }

    /**
     * 上下架服务目录
     * @param id
     */
    @Override
    public String updateStatus(long id,String status) {
        Catalogue catalogue = catalogueMapper.selectByPrimaryKey(id);

        if ("0".equals(catalogue.getStatus())&&"1".equals(status)) {//上架
            catalogue.setStatus("1");
            catalogueMapper.updateByPrimaryKeySelective(catalogue);
            return "更改服务状态为 上架";
        }
        else if ("1".equals(catalogue.getStatus())&&"0".equals(status)) {//下架
            catalogue.setStatus("0");
            catalogueMapper.updateByPrimaryKeySelective(catalogue);
            return "更改服务状态为 下架";
        }

        return "无法执行这个操作";
    }

    /**
     * 查看服务详细信息
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> details(long id) {
        Catalogue catalogue = catalogueMapper.selectByPrimaryKey(id);
        Map<String,Object> detailsMap = new HashMap<>();
        detailsMap.put("serviceName",catalogue.getName());
        detailsMap.put("serviceDescription",catalogue.getName());
        detailsMap.put("picture","暂时无法查看");
        detailsMap.put("typeName",typeMapper.selectByPrimaryKey(catalogue.getType1()).getName());
        detailsMap.put("price",catalogue.getNormalPrice());
        return detailsMap;
    }

    /**
     * 快速查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Catalogue.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap.get("serviceName")!=null&&!"".equals(searchMap.get("serviceName"))){
            criteria.andEqualTo("name",searchMap.get("serviceName"));
        }
        if (searchMap.get("code")!=null&&!"".equals(searchMap.get("code"))){
            criteria.andEqualTo("code",searchMap.get("code"));
        }
        if (searchMap.get("storeName")!=null&&!"".equals(searchMap.get("storeName"))){
            //拿到门店id 再去查询项目
            String storeName = (String) searchMap.get("storeName");
            String storeIdStr = storeMapper.findStoreId(storeName);
            long storeId = Long.parseLong(storeIdStr);
            criteria.andEqualTo("storeId",storeId);
        }
        if (searchMap.get("typeName")!="全部"&&!"".equals(searchMap.get("typeName"))){
            //通过分类名称，拿到分类id，进行查询
            String typeName = (String) searchMap.get("typeName");
            String typeId = typeMapper.findTypeId(typeName);
            criteria.andEqualTo("type1",typeId);
        }
        return example;
    }


}
